﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TByte.XnaHelper.Particles
{
    /// <summary>
    /// Single particle managed by an emitter
    /// </summary>
    public class Particle
    {
        #region Fields
        float size;
        float rotation;
        Vector2 speed = Vector2.Zero;
        float ttl = 30; //Amount of ms to live
        float lifeTime = 0; //The age of the particle in ms
        List<ParticleModifier> modifiers = new List<ParticleModifier>();
        bool firstUpdate;
        Vector2 position;
        SpriteSheet sheet;
        int sheetIndex;
        Color color;
        Emitter parent;
        #endregion

        #region Methods
        public void Update()
        {
            //Change Lifetime
            lifeTime += Manager.GameTime.ElapsedGameTime.Milliseconds;

            //Apply all modifiers
            for(int i = 0; i < modifiers.Count(); ++i)
            {
                modifiers[i].Update(this);
            }

            //Move particle
            position += speed;

            if (firstUpdate)
            {
                firstUpdate = false;
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            if (isAlive)
            {
                sheet.Draw(spriteBatch, Position, sheetIndex, color, rotation, size);
            }
        }
        public void SetSpeed(Vector2 speed)
        {
            this.speed = speed;
        }
        #endregion

        #region Constructors
        public Particle(float ttl, Vector2 position, float size, Color color, SpriteSheet sheet, int sheetIndex, List<ParticleModifier> mods, Emitter parent)
        {
            this.position = position;
            this.size = size;
            this.speed = Vector2.Zero;
            this.color = color;
            this.sheet = sheet;
            this.sheetIndex = sheetIndex;
            this.ttl = ttl * 1000;
            this.modifiers = mods;
            this.firstUpdate = true;
            this.parent = parent;
        }
        #endregion

        #region Properties
        public bool isAlive { get { if (ttl > lifeTime) { return true; } else { return false; } } }
        public Vector2 Speed { get { return speed; } set { speed = value; } }
        public float TimeToLive { get { return ttl; } set { ttl = value; } }
        public float LifeTime { get { return lifeTime; } set { lifeTime = value; } }
        public Color Color { get { return color; } set { color = value; } }
        public float Size { get { return size; } set { size = value; } }
        public float Rotation { get { return rotation; } set { rotation = value; } }
        public bool FirstUpdate { get { return firstUpdate; } }
        public Vector2 Position { get { return position; } set { position = value; } }
        public Emitter Parent { get { return parent; } }
        #endregion
    }
}
